package cn.sf.sufingadminconsumer.controller;

import cn.sf.pojo.User;
import cn.sf.sufingadminconsumer.service.AdminService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class AdminController {
  @Resource private AdminService adminService;

  @RequestMapping("/selectAdminInfoById")
  public PageInfo<User> selectAdminInfoById(
      @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum, long userRole) {
    PageHelper.startPage(pageNum, 2);
    List<User> userList = adminService.selectAdminInfoById(userRole);
    PageInfo<User> pageInfo = new PageInfo<>(userList);
    return pageInfo;
  }

  @RequestMapping("/selectAdminInfoByType")
  public List<User> selectAdminInfoByType(long roleType) {
    System.out.println("根据角色类型查询1");
    return adminService.selectAdminInfoByType(roleType);
  }

  @RequestMapping("/selectAdminInfoByEmail")
  public List<User> selectAdminInfoByEmail(String email) {

    System.out.println("根据角色邮箱地址查询");
    return adminService.selectAdminInfoByEmail(email);
  }

  @RequestMapping("/selectAdminInfoByPhone")
  public List<User> selectAdminInfoByPhone(long phone) {
    System.out.println("根据角色联系地址查询");
    return adminService.selectAdminInfoByPhone(phone);
  }

  @RequestMapping("/updateAdminInfoByUserId")
  public String updateAdminInfoByUserId(User user) {

    int result = adminService.updateAdminInfoByUserId(user);
    if (result > 0) {
      System.out.println("根据用户id进行管理员信息修改");
      return "success";
    } else {
      return "false";
    }
  }

  @RequestMapping("/insertAdminInfo")
  public String insertAdminInfo(User user) {
    int result = adminService.insertAdminInfo(user);
    if (result > 0) {
      System.out.println("添加管理员信息");
      return "success";
    } else {
      return "false";
    }
  }

  @RequestMapping("/deleteAdminByUserId")
  public String deleteAdminByUserId(long userId) {
    int result = adminService.deleteAdminByUserId(userId);
    if (result > 0) {
      System.out.println("删除管理员信息");
      return "success";
    } else {
      return "false";
    }
  }
}
